package com.ming_za.odupdaterserver.core.controller.cmd;

import com.ming_za.odupdaterserver.core.mco_core.API.CmdOperate;
import com.ming_za.odupdaterserver.core.mco_core.Cmd.CmdCreate;
import com.ming_za.odupdaterserver.core.utils.Msg;
import com.ming_za.odupdaterserver.core.utils.account.AccountManager;
import com.ming_za.odupdaterserver.core.utils.account.param.ReturnAccountPackage;

@CmdCreate(cmd = "changePassword",explain = "修改密码")
public class ChangePasswordCmd implements CmdOperate {

    private final AccountManager accountManager;
    private final Msg msg;

    public ChangePasswordCmd(AccountManager accountManager, Msg msg) {
        this.accountManager = accountManager;
        this.msg = msg;
    }

    private void showFormat(){
        Msg.getInstance().soutMsg("&c格式：changePassword <新密码>");
    }

    @Override
    public boolean onCommand(String command, String[] args) {

        if (args.length > 1){

            String password = args[1];

            if (password.length() < 6){
                msg.soutMsg("&c密码不能小于6位数");
                return false;
            }

            if (accountManager.getAllAccount().isEmpty()) {

                ReturnAccountPackage<Void> voidReturnAccountPackage = accountManager.registerAccount(password, null);
                if (voidReturnAccountPackage.isSuccess()){
                    msg.info("[登录验证]&a密码设置成功！");
                }else {
                    msg.error("[登录验证]" + voidReturnAccountPackage.getCode().getMsg());
                    return false;
                }
            }else {
                accountManager.forceModifyPassword(password);
                msg.info("[登录验证]&a密码修改成功！");
            }
            return true;
        }
        showFormat();
        return false;
    }
}
